<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-20
 * Func   彩票追号
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('lottery_order_track');
$ptable2 = Table('lottery_play');
$ctable = Table('lottery');
$utable = Table('user');

if($a == 'del')
{
	
}
else if($a == 'list')
{
	$id = intval($_GET['id']);
	$sql = "SELECT id FROM ".Table('lottery_order_main')." WHERE id = ".$id;
	$num = $db->affected_rows($sql);
	!$num && ShowMsg('内容不存在',U('user/order'));

	$sql = "SELECT u.username,l.title,
			oi.cn_play,
			k.period,
			CASE k.status
				WHEN 1 THEN
					k.code
				ELSE
					'未开奖'
			END AS code,
			oi.betting_no,oi.money,oi.num,oi.multiple,
			CASE oi.winner
				WHEN 2 THEN
					'中奖'
				WHEN 1 THEN
					'未中奖'
				ELSE
					'未开奖'
			END AS winner,
			CASE oi.chase
				WHEN 1 THEN
					'追号投注'
				ELSE
					'普通投注'
			END AS chase,
			CASE om.platform
				WHEN 1 THEN
					'pc'
				ELSE
					'mobile'
			END AS platform,
			om.createtime
			FROM yulin_lottery_order_item AS oi
			LEFT JOIN yulin_lottery_order_main AS om
			ON oi.o_id = om.id
			LEFT JOIN yulin_user AS u
			ON om.uid = u.id
			LEFT JOIN yulin_lottery AS l
			ON oi.lott_id = l.id
			LEFT JOIN yulin_lottery_k3 AS k
			ON oi.period_id = k.id
			WHERE om.id = ".$id;

	$where = "";

	$upuid = $_GET['upuid'];
	($upuid != 'all' && isset($upuid)) && $where .= " and oi.winner = ".intval($upuid);

	$period = NoBadStr($_GET['period']);
	$period && $where .= " and k.period = '".$period."'";

	$sql .= $where;
	$rows = $db->affected_rows($sql);
	if($rows){
		$page = max(1, $_GET['page']);
		$size = 20;
		$url  = "?m=".$m."&c=".$c."&a=".$a."&id=".$id."&upuid=".$upuid."&period=".$period;
		$sql .= " ORDER BY oi.id DESC LIMIT ".($page - 1) * $size.", ".$size;
		$datalist = $db->getall($sql);
		$multi = Multi($url, $page, $rows, $size);
	}

	$tpl->display("user/ordertrack_list");
}
else
{
	$id = intval($_GET['uid']);
	$sql  = "SELECT u.username,l.title,
			om.id,om.amount,om.createtime,om.code,
			CASE om.status
				WHEN 2 THEN
					'已中奖'
				WHEN 1 THEN
					'未中奖'
				ELSE
					'未开奖'
			END AS status,
			CASE om.is_chase
				WHEN 1 THEN
					'追号订单'
				ELSE
					'普通订单'
			END AS is_chase,
			CASE om.platform
				WHEN 1 THEN
					'pc'
				ELSE
					'手机'
			END AS platform
			FROM yulin_lottery_order_main AS om
			LEFT JOIN yulin_user AS u
			ON om.uid = u.id
			LEFT JOIN yulin_lottery AS l
			ON om.lott_id = l.id
			WHERE om.is_chase = 1";

	$upuid = $_GET['upuid'];
	($upuid != "all" && isset($upuid)) && $where .= " and om.status = ".intval($upuid);

	$cz = intval($_GET['cz']);
	$cz && $where .= " and l.id = ".$cz;

	$username = NoBadStr($_GET['username']);
	$username && $where .= " and u.username = '".$username."'";

	$begintime = NoBadStr($_GET['begintime']);
	$begintime && $where .= " and om.createtime > ".strtotime($begintime);

	$endtime = NoBadStr($_GET['endtime']);
	$endtime && $where .= " and om.createtime < ".strtotime($endtime);
	
	$sql .= $where;
	$rows = $db->affected_rows($sql);
	if($rows){
		$page = max(1, intval($_GET['page']));
		$size = 20;
		$url = "?m=".$m."&c=".$c."&a=".$a."&id=".$id;
		$sql .= " ORDER BY om.id DESC LIMIT ".($page - 1) * $size.", ".$size;
		$orderList = $db->getall($sql);
		$multi = Multi($url, $page, $rows, $size);
	}
	
	//	彩票种类
	$lottery = $db->getall('SELECT id,title,credit FROM '.Table('lottery').' WHERE status = 1 ORDER BY id','id');

	$tpl->display($m.'/ordertrack');

 //    $data  = array();
 //    $where = ' FROM '.$table.' t LEFT JOIN '.$utable.' u ON t.uid = u.id WHERE 1 ';
	
	// $cid = intval($_GET['cid']);
	// $cid && $where .= ' AND t.cid = '.$cid;
	
	// $uid = intval($_GET['uid']);
	// $uid && $where .= ' AND t.uid = '.$uid;
	
	// $pid = intval($_GET['pid']);
	// $pid && $where .= ' AND t.pid = '.$pid;
	
	// $title = NoBadStr($_GET['title']);
	// $title && $where .= ' AND (t.period LIKE \'%'.$title.'%\' OR u.username LIKE \'%'.$title.'%\')';

 //    $tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
 //    if($tmp['p'])
	// {
 //        $page = max(1,intval($_GET['page']));
 //        $size = 20;
 //        $limit    = ' LIMIT '.($page - 1) * $size.','.$size;

 //        $orderby = ' ORDER BY t.id DESC ';
 //        $url = '?m='.$m.'&c='.$c.'&a='.$a.'&uid='.$uid.'&cid='.$cid.'&pid='.$pid.'&title='.$title;
		
 //        $data = $db->getall('SELECT t.*,u.username '.$where.$orderby.$limit);
	// 	foreach($data as $key => $val)
	// 	{
	// 		$t2 = $db->getrow('SELECT pid,name FROM '.$ptable2.' WHERE cid = '.$val['cid'].' AND playid = '.$val['pid']);
	// 		$t3 = $db->getrow('SELECT name FROM '.$ptable2.' WHERE id = '.$t2['pid']);
			
	// 		$data[$key]['content2'] = json_decode($val['content'],true);
	// 		$data[$key]['pname'] = $t3['name'].' '.$t2['name'];
	// 	}
        
	// 	$multi = Multi($url,$page,$tmp['p'],$size);
 //    }

 //    $tpl->display($m.'/ordertrack');
}